package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.flowable.domain.BpmnConfig;
import com.ruoyi.flowable.domain.bo.BpmnConfigBo;
import com.ruoyi.flowable.domain.vo.BpmnConfigVo;
import com.ruoyi.flowable.mapper.BpmnConfigMapper;
import com.ruoyi.flowable.service.IBpmnConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 节点配置Service业务层处理
 *
 * @author zxw
 * @date 2023-03-28
 */
@RequiredArgsConstructor
@Service
public class BpmnConfigServiceImpl implements IBpmnConfigService {

    private final BpmnConfigMapper baseMapper;

    /**
     * 查询节点配置
     */
    @Override
    public BpmnConfigVo queryById(Long configId){
        return baseMapper.selectVoById(configId);
    }

    /**
     * 查询节点配置列表
     */
    @Override
    public TableDataInfo<BpmnConfigVo> queryPageList(BpmnConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BpmnConfig> lqw = buildQueryWrapper(bo);
        Page<BpmnConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询节点配置列表
     */
    @Override
    public List<BpmnConfigVo> queryList(BpmnConfigBo bo) {
        LambdaQueryWrapper<BpmnConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BpmnConfig> buildQueryWrapper(BpmnConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BpmnConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAppVersionId() != null, BpmnConfig::getAppVersionId, bo.getAppVersionId());
        lqw.eq(StringUtils.isNotBlank(bo.getNodeConfig()), BpmnConfig::getNodeConfig, bo.getNodeConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getRejectNode()), BpmnConfig::getRejectNode, bo.getRejectNode());
        return lqw;
    }

    /**
     * 新增节点配置
     */
    @Override
    public Boolean insertByBo(BpmnConfigBo bo) {
        BpmnConfig add = BeanUtil.toBean(bo, BpmnConfig.class);
        if (bo.getAppVersionId() > 0){
            BpmnConfigVo bpmnConfigVo = this.selectBpmnConfigByAppVersionId(bo.getAppVersionId());
            if (bpmnConfigVo != null){
                add.setConfigId(bpmnConfigVo.getConfigId());
                return baseMapper.updateById(add) > 0;
            }
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setConfigId(add.getConfigId());
        }
        return flag;
    }

    /**
     * 修改节点配置
     */
    @Override
    public Boolean updateByBo(BpmnConfigBo bo) {
        BpmnConfig update = BeanUtil.toBean(bo, BpmnConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BpmnConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除节点配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public BpmnConfigVo selectBpmnConfigByAppVersionId(Long appVersionId) {

        LambdaQueryWrapper<BpmnConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq( BpmnConfig::getAppVersionId, appVersionId);
        lqw.last("limit 1");
        return baseMapper.selectVoOne(lqw);
    }

    @Override
    public BpmnConfigVo queryByAppVersionId(Long appVersionId) {
        BpmnConfigBo bo = new BpmnConfigBo();
        bo.setAppVersionId(appVersionId);
        LambdaQueryWrapper<BpmnConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);
    }
}
